package com.kun.gulimall.guliproduct.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.gulimall.guliproduct.domain.PmsCategory;
import com.kun.gulimall.guliproduct.query.CategoryQuery;
import com.kun.gulimall.guliproduct.query.CategorySort;
import com.kun.gulimall.guliproduct.response.CategoryTree;
import com.kun.gulimall.guliproduct.service.PmsCategoryService;
import com.kun.gulimall.guliproduct.mapper.PmsCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @description: 商品分类
 * @author: KunKun
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory>
        implements PmsCategoryService {

    @Transactional
    @Override
    public boolean updateCategoryBatchById(List<CategorySort> categorySorts) {
        List<PmsCategory> list = categorySorts.stream().map(categorySort ->
                BeanUtil.copyProperties(categorySort, PmsCategory.class)).toList();
        return updateBatchById(list);
    }

    @Override
    public PmsCategory getCategory(Long id) {
        return getById(id);
    }

    @Override
    public boolean updateCategory(CategoryQuery pmsCategory) {
        return lambdaUpdate().set(PmsCategory::getName, pmsCategory.getName())
                .set(PmsCategory::getIcon, pmsCategory.getIcon())
                .set(PmsCategory::getProductUnit, pmsCategory.getProductUnit())
                .set(PmsCategory::getSort, pmsCategory.getSort())
                .eq(PmsCategory::getCatId, pmsCategory.getCatId()).update();
    }

    @Override
    public boolean saveCategory(@RequestBody CategoryQuery pmsCategory) {
        PmsCategory category = BeanUtil.copyProperties(pmsCategory, PmsCategory.class);
        category.setSort(pmsCategory.getSort() == null ? 0 : pmsCategory.getSort());
        category.setShowStatus(1);
        return save(category);
    }

    @Override
    public boolean removeMenuByIds(Long[] catIds) {
        // TODO 检查当前删除的菜单，是否被其他地方引用
        return removeByIds(Arrays.asList(catIds));
    }

    @Override
    public List<CategoryTree> listWithTree() {
        // 查询所有分类
        List<PmsCategory> list = list();
        // 从一级分类开始组装树形结构
        return list.stream().filter(category -> category.getParentCid() == 0).map(category -> {
            CategoryTree categoryTree = new CategoryTree(category);
            categoryTree.setChildren(getChildrens(categoryTree, list));
            return categoryTree;
            // 排序
        }).sorted(Comparator.comparingInt(CategoryTree::getSort)).toList();
    }

    public List<CategoryTree> getChildrens(CategoryTree categoryTree, List<PmsCategory> list) {
        return list.stream().filter(category -> category.getParentCid().equals(categoryTree.getCatId()))
                .map(category -> {
                    CategoryTree ca = new CategoryTree(category);
                    ca.setChildren(getChildrens(ca, list));
                    return ca;
                }).sorted(Comparator.comparing(CategoryTree::getSort, Comparator.nullsLast(Integer::compare)))
                .toList();
    }
}




